#include "index.h"
using namespace std;

void inde::build()
{
        char* dirn;
        DIR *dir = NULL;
        struct dirent *drnt = NULL;
        dirn="./";
        dir=opendir(dirn);
    
    if(dir)
    {
     int num_doc=0;
                while(drnt = readdir(dir))
                {
		             if(strstr(drnt->d_name,"txtcln")!=0) //keyword for file processing
                 {
                         
                   ifstream infile(drnt->d_name);  //VVOD IS FAILA
                   string buf;
                   while(getline(infile,buf))           
                   data[buf].insert(num_doc); 
                   doc_name[num_doc]=drnt->d_name;
                   num_doc++;
                }
         //      return;
               }
                closedir(dir);
    }
        else
        {
                printf("Can not open directory '%s'\n", dirn);
        }

}

 
//void where(istream &istr )
void inde::where(string str)
{
string s ;
set <string> l;
istringstream istr(str);
int number_of_words_searched=0;
 while(istr >> s)
 {cout<<s<<endl;
  l.insert(s); //zapolnyaem
  number_of_words_searched++;
   if(data.find(s)!=data.end()) //proverka na validnost'
   {
   cout<<"found"<<endl;
     
   }
   else
   { cout<<"not found. search failed "<<endl; return; }
  }
//proverim, odno slovo ishem ili net
  if(number_of_words_searched==1)
  {for(set < int >::iterator si=data[s].begin();si!=data[s].end(); si++ )
   cout<<*si<<" ";
   return;
  }
 set < string >::iterator prev;
 set < int > result;
 set < int > tmp; 
 for(set <string>::iterator it=l.begin(); it!=l.end(); it++)
 {
  if(it==l.begin())
  {
  prev=it;
 // cout<<"!1 "<<*it<<endl;
  }
  else
  {
  // cout<<"!2 "<<*it<<endl;
   if(result.size()==0)
   {
  // cout<<"in if ";
   set_intersection(data[*it].begin(),data[*it].end(),
   data[*prev].begin(),data[*prev].end(),inserter(result,result.begin()));
   //cout<<result.size()<<endl;
   }
   else 
   {
   //cout<<"in else ";
   set_intersection(data[*it].begin(),data[*it].end(),
   result.begin(),result.end(),inserter(tmp, tmp.begin()));
   result=tmp;
   tmp.clear();
   //cout<<result.size()<<endl;
   }
  prev=it;
  }
for( set< int >::iterator sit=result.begin();sit!=result.end();sit++ )
cout<<*sit<<endl; 

}
 
}

void inde::save() //zapis v fail
{ofstream ofile;
 ofile.open("index.txt");
 //int num_of_lines=;
  for(map < string,set <int>  >::iterator mssit=data.begin(); mssit!=data.end(); mssit++)
  {
  ofile<<mssit->first<<endl;
    for(set<int>::iterator sit = mssit->second.begin(); sit != mssit->second.end(); sit++ )
    ofile<<*sit<<endl;
  }
ofile<<"-"<<endl;
 for(map< int ,string >::iterator misit=doc_name.begin(); misit!= doc_name.end(); misit++)
 ofile<<misit->first<<'\n'<<misit->second<<'\n';
ofile.close();
}
void inde::load()
{
ifstream infile("index.txt");
char s[64];
char key[64];
while(infile.getline(s,64))
 {
  if(s[0]!='-')
  {
   if(!isdigit(s[0]))
   {
   strcpy(key,s);
   }
   else data[key].insert(atoi(s)); 
  
  }
  else 
  {
  int order=0;
   while(infile.getline(s,64))
   {
    if(order%2)
    {doc_name[atoi(key)]=s;
    //cout<<key<< " -  "<<s<<endl;
    order++;
    }
    else
    {
    strcpy(key,s);
    order++;  
    }
   }
return;
  
  }
 }
infile.close();
}

void inde::print()
{

cout<<"lovely index"<<endl;
map <string , set<int> >::iterator it;
 for(it=data.begin(); it!=data.end(); it++)
 {cout<<"["<<it->first<<"]"<<endl;
   for(set <int>::iterator i=(it->second).begin();i!=(it->second).end();i++) 
   cout<<"    "<<*i<<" "<<doc_name[*i]<<endl;

  }


}


